import com.dmotiko.chabeli.*
import com.dmotiko.general.*
import com.dmotiko.util.*
import mx.transitions.easing.Regular;
import mx.transitions.Tween;
import mx.utils.Delegate

class ZoomSlider
extends BaseClip {
	
	private var mcDrag:MovieClip;
	private var mcTrigger:MovieClip;
	private var mcBar:MovieClip;
	private var mcPrev:MovieClip;
	private var mcNext:MovieClip;
	
	private function ZoomSlider() {
		super();
		//addEventListener( BaseClipEvent.CHANGED, function( e ) { trace(e.data) } );
	}
	
	private function initClip():Void {
		var root:ZoomSlider = this;
		if (!mcTrigger) mcTrigger = mcDrag;
		mcTrigger.offsetX = mcDrag._x - mcTrigger._x;
		mcTrigger.offsetY = mcDrag._y - mcTrigger._y;
		mcTrigger.onPress = function() {
			root.mcDrag.startDrag( false, root.mcBar._x, 0, root.mcBar._x + root.mcBar._width - root.mcDrag._width, 0);
			root.mcDrag.onMouseMove = function() {
				root.mcTrigger._x = root.mcDrag._x - root.mcTrigger.offsetX;
				var nPercent:Number = (root.mcDrag._x - root.mcBar._x) * 100 / (root.mcBar._width - root.mcDrag._width);
				if ( nPercent < 0 ) nPercent = 0;
				root.setData( Math.round(nPercent) );
			}
		}
		mcTrigger.onReleaseOutside = mcTrigger.onRelease = function() {
			this.stopDrag();
			delete this.onMouseMove;
		}
		mcPrev.onRelease = function() {
			var nX:Number;
			nX = root.mcDrag._x - 30;
			if ( nX < root.mcBar._x ) nX = root.mcBar._x;
			if ( nX == root.mcDrag._x ) return;
			root.registerTween("dragMove", new Tween( root.mcDrag, "_x", Regular.easeInOut, root.mcDrag._x, nX, 0.5, true ), false, false, true);
		}
		mcNext.onRelease = function() {
			var nX:Number;
			nX = root.mcDrag._x + 30;
			if ( nX > root.mcBar._x + root.mcBar._width - root.mcDrag._width ) nX = root.mcBar._x + root.mcBar._width - root.mcDrag._width;
			if ( nX == root.mcDrag._x ) return;
			root.registerTween("dragMove", new Tween( root.mcDrag, "_x", Regular.easeInOut, root.mcDrag._x, nX, 0.5, true ), false, false, true);
			
		}
	}
	
	private function tweenChanged(key:String, tween:Tween):Void {
		mcTrigger._x = mcDrag._x - mcTrigger.offsetX;
		var nPercent:Number = (mcDrag._x - mcBar._x) * 100 / (mcBar._width - mcDrag._width);
		if ( nPercent < 0 ) nPercent = 0;
		setData( Math.round(nPercent) );
	}
	
	private function refreshData():Void {
		dispatchEvent( { type: BaseClipEvent.CHANGED, data: this.data, target: this } );
	}
	
}